import connection from "@/config/database";
import { Tables } from "@/db/tables/tables-name-enum";
import db from "@/db/knex";

class UserMapper {
    async create(user) {
        // 1.获取用户 user
        const { name, password } = user;

        // 2.拼接statement
        const statement = `INSERT INTO ${Tables.USER} (user_name, user_pwd) VALUES (?, ?);`;

        // 3.执行sql语句
        // const [result] = await connection.execute(statement, [name, password]);
        // return result;
        const created_by = 111111;
        const updated_by = 22222222;
        const [userId] = await db
            .insert({ user_name: name, user_pwd: password, user_code: "kunkun", created_by, updated_by })
            .into(Tables.USER);
        return userId;
    }

    async findUserByName(name) {
        const statement = `SELECT * FROM ${Tables.USER} WHERE user_name = ?;`;
        console.log("statement", statement);
        const res = await db.select("*").from(Tables.USER).where({ user_name: name });
        return res;
        // const [values] = await connection.execute(statement, [name]);
        // return values;
    }

    async updateUserAvatar(avatarUrl, userId) {
        const statement = "UPDATE user SET avatar_url = ? WHERE user.id = ?;";
        // const [values] = await connection.execute(statement, [avatarUrl, userId]);
        // return values;
    }

    async getUserById(userId) {
        const statement = `SELECT * FROM ${Tables.USER} WHERE user_id = ?;`;
        const res = await db.select("*").from(Tables.USER).where({ user_id: userId });
        return res;
    }

    async updateUserPassword(password, userId) {
        const statement = "UPDATE user SET password = ? WHERE id = ?;";
        // const [values] = await connection.execute(statement, [password, userId]);
        // return values;
    }

    async deleteUserById(userId) {
        const statement = "DELETE FROM user WHERE id = ?;";
        // const [values] = await connection.execute(statement, [userId]);
        // return values;
    }
}

export default new UserMapper();
